/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dell
 * Date: 2023-12-13
 * Time: 21:52
 */
public class MysingArrray {
    class Node {
        int vale;
        Node next;

        public Node(int vale) {
            this.vale = vale;
        }
    }

    Node head;
    Node last;

    public void listArrray() {
        Node node1 = new Node(12);
        Node node2 = new Node(23);
        Node node3 = new Node(34);
        Node node4 = new Node(45);
        Node node5 = new Node(56);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;
    }

    public void show() {
        Node card = head;
        while (card != null) {
            System.out.print(card.vale + " ");
            card = card.next;
        }
        System.out.println();
    }

    public int size() {
        Node card = head;
        int size = 0;
        while (card != null) {
            card = card.next;
            size++;
        }
        return size;
    }

    public boolean contains(int key) {
        Node card = head;
        while (card != null) {
            if (card.vale == key) {
                return true;
            }
            card = card.next;
        }
        return false;
    }

    public void addFiest(int data) {
        Node node = new Node(data);
        node.next = head;
        head = node;
    }

    public void addLast(int data) {
        Node node = new Node(data);
        if (head == null) {
            head = node;
        }
        Node card = head;
        while (card.next != null) {
            card = card.next;
        }
        ;
        card.next = node;
    }

    public void addfreee(int tmp, int data) {
        if (tmp < 0 || tmp > this.size()) {
            throw new IndexOutofBounds("错误");

        } else if (tmp == 0) {
            this.addFiest(data);
            return;
        } else if (tmp == size()) {
            this.addLast(data);
            return;
        }
        Node node = new Node(data);
        Node card = head;
        while (tmp - 1 != 0) {
            card = card.next;
            tmp--;
        }
        node.next = card.next;
        card.next = node;
    }

    public void remove(int key) {
        if (head == null) {
            return;
        }
        if (head.vale == key) {
            head = head.next;
            return;
        }
        Node card = searchPrey(key);
        if (card == null) {
            System.out.println("没有这个结点" + key);
            return;
        }
        Node tmp = card.next;
        card.next = tmp.next;


    }

    public void removeallkey(int key) {
//        if (head==null){
//            return;
//        }
//        while(head.vale!=key){
//            head=head.next;
//            return;
//        }
        Node card = head;
        Node tmp = head;
        while (card != null) {
//            if (card.vale==key){
//                tmp.next=card.next;
//            }else {
//                tmp=card;
//            }
            if (card.vale != key) {
                tmp = card;
            } else {
                tmp.next = card.next;
            }
            card = card.next;
        }
        if (head.vale == key) {
            head = head.next;
            return;
        }
    }

    private Node searchPrey(int key) {
        Node tmp = head;
        while (tmp.next != null) {
            if (tmp.next.vale == key) {
                return tmp;
            } else {
                tmp = tmp.next;
            }
        }
        return null;
    }
//    public MysingArrray removeNthFromEnd(MysingArrray head, int n) {
//        MysingArrray fast=head;
//        MysingArrray slow=head;
//        if (head==null){
//            return null;
//        }
//        int m=n+1;
//        while(m>0){
//            m--;
//            fast=fast.next;
//        }
//        while(fast!=null){
//            fast=fast.next;
//            slow=slow.next;
//        }
//        return slow;
//    }
}